package com.yuexun.dhdoctor.module.healthRecord.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Filter;
import android.widget.Filterable;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.module.healthRecord.bean.HealthRcordBean;
import com.yuexun.dhdoctor.module.healthRecord.ui.Act_HealthRecordDetail;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by work-jx on 2016/4/14.
 */
public class HealthRecordAdapter extends KJAdapter<HealthRcordBean.RowsBean> implements Filterable{

    private PatientFilter filter;
    private ArrayList<HealthRcordBean.RowsBean> healthRecord_infos = new ArrayList<>();
    ArrayList<HealthRcordBean.RowsBean> healthRecord_info_filter = new ArrayList<>();

    public HealthRecordAdapter(AbsListView view, Collection<HealthRcordBean.RowsBean> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        healthRecord_infos = (ArrayList<HealthRcordBean.RowsBean>) ((ArrayList)mDatas).clone();
    }

    @Override
    public void convert(AdapterHolder adapterHolder, HealthRcordBean.RowsBean item, boolean isScrolling) {
        adapterHolder.setText(R.id.importmentpatient_name, item.getName());
        adapterHolder.setText(R.id.importmentpatient_archive,item.getPerson_id());
        adapterHolder.setText(R.id.importmentpatient_age,item.getAge()+"");
        onPicClick(adapterHolder.getConvertView(), item);
    }

    private void onPicClick(View view, final HealthRcordBean.RowsBean item) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("HealthRecordListBean",item);
                Intent intent = new Intent(mCxt,Act_HealthRecordDetail.class);
                intent.putExtras(bundle);
                mCxt.startActivity(intent);
            }
        });
    }

    @Override
    public Filter getFilter() {
        if(filter == null){
            filter = new PatientFilter();
        }
        return filter;
    }

    class PatientFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();
            if(constraint == null || constraint.length() == 0){
                results.values = healthRecord_infos;
                results.count = healthRecord_infos.size();
            }else{
                healthRecord_info_filter.clear();
                for(HealthRcordBean.RowsBean p : healthRecord_infos){
                    if(p.getName().contains(constraint.toString())){
                        healthRecord_info_filter.add(p);
                    }
                }
                results.values = healthRecord_info_filter;
                results.count = healthRecord_info_filter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            refresh((Collection<HealthRcordBean.RowsBean>) results.values);
        }
    }
}
